package com.atguigu.chapter08;

import com.atguigu.bean.UserBehavior;
import com.atguigu.util.AnqclnUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * Author: Pepsi
 * Date: 2023/8/14
 * Desc:
 */
public class Flink02_Project_High_Pv_process {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 1000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        env
                .readTextFile("input/UserBehavior.csv")
                .map(line -> {
                    String[] data = line.split(",");
                    return new UserBehavior(
                            Long.valueOf(data[0]),
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2]),
                            data[3],
                            // valueOf和parseLong的区别是返回值类型，前面是包装类，后面是long，建议分开用，减少自动装箱拆箱的性能浪费
                            Long.parseLong(data[4])
                    );
                })
                .filter(ub -> "pv".equals(ub.getBehavior()))
                // 分配时间戳和水印
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy
                                // 乱序程度为3s
                                .<UserBehavior>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                // 指定时间戳  ，对象里面的时间戳是秒，转成毫秒
                                .withTimestampAssigner((element, recordTimestamp) -> element.getTimestamp() * 1000)
                )
                // 开窗，开一个滑动窗口，窗口大小2小时，滑动步长1小时
                .windowAll(SlidingEventTimeWindows.of(Time.hours(2), Time.hours(1)))
                .process(new ProcessAllWindowFunction<UserBehavior, String, TimeWindow>() {

                    private AggregatingState<UserBehavior, Long> pvState;

                    @Override
                    public void open(Configuration parameters) throws Exception {
                        pvState = getRuntimeContext().getAggregatingState(
                                new AggregatingStateDescriptor<UserBehavior, Long, Long>(
                                        "countUB",
                                        new AggregateFunction<UserBehavior, Long, Long>() {
                                            @Override
                                            public Long createAccumulator() {
                                                return 0L;
                                            }

                                            @Override
                                            public Long add(UserBehavior value, Long accumulator) {
                                                return accumulator + 1;
                                            }

                                            @Override
                                            public Long getResult(Long accumulator) {
                                                return accumulator;
                                            }

                                            @Override
                                            public Long merge(Long a, Long b) {
                                                return null;
                                            }
                                        },
                                        Types.LONG
                                )
                        );

                    }

                    @Override
                    public void process(Context context,
                                        Iterable<UserBehavior> elements,
                                        Collector<String> out) throws Exception {
                        // 用之前要把上一个窗口的状态清空，要不然就是累加了
                        // 因为这是键控状态，只和键有关，所以 只是 同一个key的不同的窗口用的同一个状态，所以要想每个窗口用自己的就要clear一下
                        pvState.clear();

                        for (UserBehavior element : elements) {
                            pvState.add(element);
                        }

                        String startTime = AnqclnUtil.toDateTime(context.window().getStart());
                        String endTime = AnqclnUtil.toDateTime(context.window().getEnd());


                        out.collect(startTime+" - "+endTime+" - "+ pvState.get());
                    }
                })
                .print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
